package com.koushikdutta.async.http.filter;

import com.koushikdutta.async.ByteBufferList;
import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.NullDataCallback;
import com.koushikdutta.async.PushParser;
import com.koushikdutta.async.PushParser.ParseCallback;
import com.koushikdutta.async.callback.DataCallback;
import com.koushikdutta.async.http.libcore.Memory;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.zip.CRC32;
import java.util.zip.Inflater;

public class GZIPInputFilter extends InflaterInputFilter {
    protected CRC32 crc = new CRC32();
    boolean mNeedsHeader = true;

    public GZIPInputFilter() {
        super(new Inflater(true));
    }

    public void onDataAvailable(final DataEmitter emitter, ByteBufferList bb) {
        if (this.mNeedsHeader) {
            final PushParser parser = new PushParser(emitter);
            parser.readByteArray(10, new ParseCallback<byte[]>() {
                int flags;
                boolean hcrc;

                public void parsed(byte[] header) {
                    boolean z = true;
                    if (Memory.peekShort(header, 0, ByteOrder.LITTLE_ENDIAN) != (short) -29921) {
                        GZIPInputFilter.this.report(new IOException(String.format("unknown format (magic number %x)", new Object[]{Short.valueOf(magic)})));
                        emitter.setDataCallback(new NullDataCallback());
                        return;
                    }
                    this.flags = header[3];
                    if ((this.flags & 2) == 0) {
                        z = false;
                    }
                    this.hcrc = z;
                    if (this.hcrc) {
                        GZIPInputFilter.this.crc.update(header, 0, header.length);
                    }
                    if ((this.flags & 4) != 0) {
                        parser.readByteArray(2, new ParseCallback<byte[]>() {
                            public void parsed(byte[] header) {
                                if (AnonymousClass1.this.hcrc) {
                                    GZIPInputFilter.this.crc.update(header, 0, 2);
                                }
                                parser.readByteArray(Memory.peekShort(header, 0, ByteOrder.LITTLE_ENDIAN) & 65535, new ParseCallback<byte[]>() {
                                    public void parsed(byte[] buf) {
                                        if (AnonymousClass1.this.hcrc) {
                                            GZIPInputFilter.this.crc.update(buf, 0, buf.length);
                                        }
                                        AnonymousClass1.this.next();
                                    }
                                });
                            }
                        });
                    } else {
                        next();
                    }
                }

                private void next() {
                    PushParser parser = new PushParser(emitter);
                    DataCallback summer = new DataCallback() {
                        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                            if (AnonymousClass1.this.hcrc) {
                                while (bb.size() > 0) {
                                    ByteBuffer b = bb.remove();
                                    GZIPInputFilter.this.crc.update(b.array(), b.arrayOffset() + b.position(), b.remaining());
                                    ByteBufferList.reclaim(b);
                                }
                            }
                            bb.recycle();
                            AnonymousClass1.this.done();
                        }
                    };
                    if ((this.flags & 8) != 0) {
                        parser.until((byte) 0, summer);
                    } else if ((this.flags & 16) != 0) {
                        parser.until((byte) 0, summer);
                    } else {
                        done();
                    }
                }

                private void done() {
                    if (this.hcrc) {
                        parser.readByteArray(2, new ParseCallback<byte[]>() {
                            public void parsed(byte[] header) {
                                if (((short) ((int) GZIPInputFilter.this.crc.getValue())) != Memory.peekShort(header, 0, ByteOrder.LITTLE_ENDIAN)) {
                                    GZIPInputFilter.this.report(new IOException("CRC mismatch"));
                                    return;
                                }
                                GZIPInputFilter.this.crc.reset();
                                GZIPInputFilter.this.mNeedsHeader = false;
                                GZIPInputFilter.this.setDataEmitter(emitter);
                            }
                        });
                        return;
                    }
                    GZIPInputFilter.this.mNeedsHeader = false;
                    GZIPInputFilter.this.setDataEmitter(emitter);
                }
            });
            return;
        }
        super.onDataAvailable(emitter, bb);
    }
}
